package com.edu.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.edu.system.service.ITPublishClassService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.TNoticeMapper;
import com.edu.system.domain.TNotice;
import com.edu.system.service.ITNoticeService;

/**
 * 通知公告Service业务层处理
 *
 * @author xxxx
 * @date 2023-12-31
 */
@Service
public class TNoticeServiceImpl implements ITNoticeService
{
    @Autowired
    private TNoticeMapper tNoticeMapper;
    @Resource
    private ITPublishClassService publishClassService;

    // 1通知 2校园风采
    public static Integer publishClass_type=1;

    /**
     * 查询通知公告
     *
     * @param id 通知公告主键
     * @return 通知公告
     */
    @Override
    public TNotice selectTNoticeById(Long id)
    {
        return tNoticeMapper.selectTNoticeById(id);
    }

    /**
     * 查询通知公告列表
     *
     * @param tNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<TNotice> selectTNoticeList(TNotice tNotice)
    {
        return tNoticeMapper.selectTNoticeList(tNotice);
    }

    /**
     * 新增通知公告
     *
     * @param tNotice 通知公告
     * @return 结果
     */
    @Override
    public int insertTNotice(TNotice tNotice)
    {
        return tNoticeMapper.insertTNotice(tNotice);
    }

    /**
     * 修改通知公告
     *
     * @param tNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateTNotice(TNotice tNotice)
    {
        return tNoticeMapper.updateTNotice(tNotice);
    }

    @Override
    public int publishStatusToOn(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return 0;
        }
        return tNoticeMapper.publishByIds(ids,
            TNotice.builder().publishStatus(publishStatus_on).publishTime(new Date()).build()
        );
    }

    /**
     * 批量删除通知公告
     *
     * @param ids 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTNoticeByIds(Long[] ids)
    {
        Stream.of(ids).forEach(id -> publishClassService.deleteTPublishClass(id, publishClass_type));
        return tNoticeMapper.deleteTNoticeByIds(ids);
    }

    /**
     * 删除通知公告信息
     *
     * @param id 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTNoticeById(Long id)
    {
        publishClassService.deleteTPublishClass(id, publishClass_type);
        return tNoticeMapper.deleteTNoticeById(id);
    }

    @Override
    public List<TNotice> liveListByIds(List<Long> ids, Integer noticeType) {
        if (CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        return tNoticeMapper.listByIds(ids, noticeType)
            .stream()
            .filter(a -> a.getNoticeStartTime() != null && new Date().getTime() >= a.getNoticeStartTime().getTime())
            .filter(a -> a.getNoticeEndTime() != null && new Date().getTime() <= a.getNoticeEndTime().getTime())
            .collect(Collectors.toList());
    }

    @Override
    public List<TNotice> listByIds(List<Long> ids, Integer noticeType) {
        if (CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        return tNoticeMapper.listByIds(ids, noticeType);
    }


   // "发布状态 0:待发布 1:已发布"
    public static Integer publishStatus_on=1;
    public static Integer publishStatus_off=1;

    /**
     * 状态 0:下架 1:上架
     */
    //"状态 0:下架 1:上架"
    public static Integer status_on = 1;
    public static Integer status_off = 1;
    /**
     * 给websocket推拉消息
     * @return
     */
    public List<TNotice> selectTNoticeForWS(){
        TNotice query = new TNotice();
        Date now = new Date();
        query.setNoticeEndTime(now);
        query.setNoticeStartTime(now);
        query.setStatus(status_on);
        query.setPublishStatus(publishStatus_on);
        return this.selectTNoticeList(query);
    }

    @Override
    public List<TNotice> queryTodayChangeList() {
        Date date = new Date();
        DateTime beginTime = DateUtil.beginOfDay(date);
        DateTime endTime = DateUtil.endOfDay(date);
        return tNoticeMapper.needStatusChangeList(beginTime, endTime);
    }


}
